/**
 * Created by narnia on 2017/4/26.
 */
function Lancer() {
    lancer = this;
    this.windowHeight = $(window).height();
    this.windowWidth = $(window).width();
    this.appendEvent();
    this.naviBar();
    this.sideBar();
    this.popup();
    this.fragment();
    this.tab();
    this.back();
}
/**
 * 页面元素追加事件
 */
Lancer.prototype.appendEvent = function () {
    // ajaxtarget
    $('body').append('<iframe id="ajaxtarget" name="ajaxtarget" style="display: none"></iframe>');
    // 页面
    $('.la-page-group').height(lancer.windowHeight).width(lancer.windowWidth);
}
/**
 * 横向菜单自动匹配
 */
Lancer.prototype.naviBar = function () {
    var naviBarDl = $('.nav-bar-dl');
    var naviBarDd = naviBarDl.find('dd');
    naviBarDd.css({'width': naviBarDl.parent().width() / naviBarDd.length});
}
/**
 * 侧栏
 */
Lancer.prototype.sideBar = function () {
    var sideBar = $('.la-side-bar');
    var sidebarLeft = $('.la-side-bar-left');
    var sidebarRight = $('.la-side-bar-right');
    var sideBarOpen = $('.la-side-bar-open');
    var sideBarClose = $('.la-side-bar-close');
    var side;
    sideBar.height(lancer.windowHeight).width(lancer.windowWidth - lancer.windowWidth / 2);
    sidebarRight.css({'right':'-'+sidebarRight.width()+'px'});
    sidebarLeft.css({'left': '-' + sidebarLeft.width() +'px'});
    sideBarOpen.click(function () {
        // 关联侧栏，如果侧栏class存在,显示侧栏
        side = $('.' + $(this).data('side'));
        if (side) {
            if (side.hasClass('la-side-bar-right')) {
                // 右侧
                side.css({
                    'display': 'block',
                    'transform': 'translateX(-' + sidebarRight.width() + 'px)'
                });
            }
            if (side.hasClass('la-side-bar-left')) {
                // 左侧
                side.css({
                    'display': 'block',
                    'transform': 'translateX('+sidebarLeft.width() + 'px)'
                });
            }
        }
    });
    sideBarClose.click(function () {
        side = $('.' + $(this).data('side'));
        lancer.closeSideEvent(side);
    });
}
/**
 * 关闭侧栏事件
 * @param side  侧栏jquery对象
 */
Lancer.prototype.closeSideEvent = function (o) {
    // 如果侧栏存在，隐藏侧栏
    if (o.hasClass('la-side-bar-right')) {
        // 右侧
        o.css({'transform':'translateX('+o.width()+'px)'});
    }
    if (o.hasClass('la-side-bar-left')) {
        // 左侧
        o.css({'transform':'translateX(-'+o.width()+'px)'});
    }
    setTimeout(function () {
        o.hide();
    },500);
}
/**
 * 关闭侧栏
 */
Lancer.prototype.closeSide = function (cls) {
    cls = $('.' + cls);
    if (cls.length >= 1 && cls.css('display') == 'block') {
        lancer.closeSideEvent(cls);
    }
}
/**
 * popup
 */
Lancer.prototype.popup = function () {
    var popup = $('.la-popup');
    var popupOpen = $('.la-popup-open');
    var popupClose = $('.la-popup-close');
    var o;
    popup.height(lancer.windowHeight).width(lancer.windowWidth);
    popup.css({'bottom':'-'+popup.height()+'px'});
    popupOpen.click(function () {
        o = $('.' + $(this).data('popup'));
        o.css({
            'display': 'block',
            'transform': 'translateY(-' + o.height() + 'px)'
        });
    });
    popupClose.click(function () {
        o = $('.' + $(this).data('popup'));
        lancer.closePopupEvent(o);
    });
}
/**
 * 关闭popup
 * @param o popup对象
 */
Lancer.prototype.closePopupEvent = function (o) {
    o.css({'transform':'translateY('+o.height()+'px)'});
    setTimeout(function () {
        o.hide();
    },500);
}
/**
 * 打开popup
 */
Lancer.prototype.openPopup = function (e,func) {
    var o = $('.' + e.dataset['popup']);
    o.css({
        'display': 'block',
        'transform': 'translateY(-' + o.height() + 'px)'
    });
    if (func) {
        func();
    }
}
Lancer.prototype.closePopup = function (cls) {
    cls = $('.' + cls); 
    if (cls.length >= 1 && cls.css('display') == 'block') {
        lancer.closePopupEvent(cls);
    }
}
/**
 * fragment
 */
Lancer.prototype.fragment = function () {
    var fragment = $('.la-fragment');
    var fragmentOpen = $('.la-fragment-open');
    var fragmentClose = $('.la-fragment-close');
    var o;
    fragment.height(lancer.windowHeight).width(lancer.windowWidth);
    fragment.css({'right':'-'+fragment.width()+'px'});
    fragmentOpen.click(function () {
        o = $('.' + $(this).data('fragment'));
        o.css({
            'display': 'block',
            'transform': 'translateX(-' + o.width() + 'px)'
        });
    });
    fragmentClose.click(function () {
        o = $('.' + $(this).data('fragment'));
        lancer.closeFragmentEvent(o);
    });
}
/**
 * 打开fragment
 */
Lancer.prototype.openFragment = function (e,func) {
    var o = $('.' + e.dataset['fragment']);
    o.css({
        'display': 'block',
        'transform': 'translateX(-' + o.width() + 'px)'
    });
    if (func) {
        func();
    }
}
/**
 * 关闭fragment事件
 * @param side  侧栏jquery对象
 */
Lancer.prototype.closeFragmentEvent = function (o) {
    // 如果侧栏存在，隐藏侧栏
    o.css({'transform':'translateX('+o.width()+'px)'});
    setTimeout(function () {
        o.hide();
    },500);
}
/**
 * 关闭fragment
 * @param cls 
 */
Lancer.prototype.closeFragment = function (cls) {
    cls = $('.' + cls);
    if (cls.length >= 1 && cls.css('display') == 'block') {
        lancer.closeFragmentEvent(cls);
    }
}
/**
 * tab切换
 */
Lancer.prototype.tab = function () {
    var o;
    var f;
    $('.la-tab-open').click(function () {
        o = $(this);
        f = $('.' + o.data('tab-for'));
        // 隐藏
        $('.' + f.data('tab-hidden')).removeClass('la-tab-activity');
        // 显示
        $('.' + o.data('tab')).addClass('la-tab-activity');
        // 移除点击控件的样式
        $('.' + f.data('tab-control')).removeClass(f.data('tab-click-class'));
        // 设置当前点击的样式
        o.addClass(f.data('tab-click-class'));
    })
}
/**
 * 后退
 */
Lancer.prototype.back = function () {
    // 后退
    var laBack = $('.la-back');
    if (laBack.length >= 1) {
        laBack.click(function () {
            if (!$(this).attr('href')) {
                window.history.back(-1);
            }
        });
    }
}
/**
 * 微信上拉刷新
 */
Lancer.prototype.pullup = function (cls,func) {
    var e = document.getElementsByClassName(cls)[0];
    var o = $(e);
    var scrollTop = 0;
    // 出发时y的坐标
    var y = 0;
    var y1 = 0;
    o.scroll(function () {
        scrollTop = e.scrollTop
    });
    e.addEventListener('touchstart',function (event) {
        y = event.changedTouches[0].pageY;
        // if (scrollTop == 0) {
        //     y = event.changedTouches[0].pageY;
        // }
    });
    e.addEventListener('touchend',function (event) {
        if (y1 < y) {
            if (scrollTop + o.height() + 1 >= o[0].scrollHeight - o[0].scrollHeight / 4) {
                func();
            }
        }
    });


    // var o = $('.' + cls);
    // var nH = o.data('negative-height') ? parseInt(o.data('negative-height')) : 0;
    // var f = true;
    // o.scroll(function () {
    //     if (o[0].scrollTop + o.height() >= o[0].scrollHeight) {
    //         func();
    //     }
    // });
}
/**
 * 微信下拉加载
 */
Lancer.prototype.pulldown = function (cls,func) {
    var e = document.getElementsByClassName(cls)[0];
    var o = $(e);
    var scrollTop = 0;
    o.scroll(function () {
        scrollTop = e.scrollTop
    });
    // 出发时y的坐标
    var y = 0;
    var y1 = 0;
    e.addEventListener('touchstart',function (event) {
        y = event.changedTouches[0].pageY;
        // if (scrollTop == 0) {
        //     y = event.changedTouches[0].pageY;
        // }
    });
    e.addEventListener('touchend',function (event) {
        y1 = event.changedTouches[0].pageY;
        if (scrollTop == 0) {
            y1 = event.changedTouches[0].pageY;
            if (y1 > y) {
                func();
            }
        }
    });
}
function isEmpty(str) {
    if (str.replace(/\s*/ig, "").length ==0)
    {
        return true;
    }
}
